package com.szyt.basicframe.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ResponseResult<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -2797326301052197337L;

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String msg;
    /**
     * 查询到的结果数据，
     */
    private T data;

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 成功响应
    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(200, StatusEnum.SUCCESS.getMsg(), data);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(StatusEnum.SUCCESS.getCode(), message, data);
    }

    // 失败响应
    public static <T> ResponseResult<T> error() {
        return error(StatusEnum.INTERNAL_SERVER_ERROR.getCode(), StatusEnum.INTERNAL_SERVER_ERROR.getMsg());
    }

    public static <T> ResponseResult<T> error(String message) {
        return error(StatusEnum.INTERNAL_SERVER_ERROR.getCode(), message);
    }

    public static <T> ResponseResult<T> error(int code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    public static <T> ResponseResult<T> error(int code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }

    public static <T> ResponseResult<T> error(StatusEnum status) {
        return new ResponseResult<>(status.getCode(), status.getMsg());
    }
}
